package com.example.battle.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.battle.R;
import com.example.battle.activity.Register;

public class BattleFragment extends Fragment {
    Button front_up,front_down,back_up,back_down;
    TextView battle_title,front_flag,back_flag,front_talk,back_talk;
    int frontNum = 0;
    int backNum = 0;
    final int TIMER_MSG = 0x11;

    String talk1[] = {"This is right","I agree with it","I think so"};
    String talk2[] = {"I don't think so","I must say something","That's too bad"};
    final int talk1_length = talk1.length;
    final int talk2_length = talk2.length;
    int begin1 = 0;
    int begin2 = 0;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_battle, container, false);


        front_up = root.findViewById(R.id.front_up);
        front_down = root.findViewById(R.id.front_down);
        back_up = root.findViewById(R.id.back_up);
        back_down = root.findViewById(R.id.back_down);
        battle_title = root.findViewById(R.id.battle_title);
        front_flag = (TextView)root.findViewById(R.id.front_flag);
        back_flag = (TextView)root.findViewById(R.id.back_flag);
        front_talk = root.findViewById(R.id.front_talk);
        back_talk = root.findViewById(R.id.back_talk);

        Init();

        handler.sendEmptyMessage(TIMER_MSG);
        //设置监听器
        front_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                frontNum++;
                front_flag.setText(String.valueOf(frontNum));
            }
        });
        front_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                frontNum--;
                front_flag.setText(String.valueOf(frontNum));
            }
        });
        back_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backNum++;
                back_flag.setText(String.valueOf(backNum));
            }
        });
        back_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backNum--;
                back_flag.setText(String.valueOf(backNum));

            }
        });
        return root;
    }

    private void Init()
    {
        front_flag.setText(String.valueOf(frontNum));
        back_flag.setText(String.valueOf(backNum));
        front_talk.setVisibility(View.INVISIBLE);
        back_talk.setVisibility(View.INVISIBLE);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message){
            super.handleMessage(message);
            if (talk1_length-begin1>0){
                String talk = talk1[begin1];
                front_talk.setText(talk);
                front_talk.setVisibility(View.VISIBLE);
                handler.sendEmptyMessageDelayed(TIMER_MSG,2000);
                if (begin1==talk1_length-1){
                    begin2 = 0;
                    front_talk.setVisibility(View.INVISIBLE);
                }
                begin1++;
            }
            else {
                String talk = talk2[begin2];
                back_talk.setText(talk);
                back_talk.setVisibility(View.VISIBLE);
                handler.sendEmptyMessageDelayed(TIMER_MSG,2000);
                if (begin2==talk2_length-1){
                    begin1 =0;
                    back_talk.setVisibility(View.INVISIBLE);
                }
                begin2++;
            }
        }
    };
}
